/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_order.repository;

import com.yuecai.modules.yc_order.domain.YcOrder;
import com.yuecai.modules.yc_order.service.dto.YcOrderQueryCriteria;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

/**
* @website https://el-admin.vip
* @author DM
* @date 2020-06-26
**/
public interface YcOrderRepository extends JpaRepository<YcOrder, Long>, JpaSpecificationExecutor<YcOrder> {
    /**
    * 根据 OrderSn 查询
    * @param order_sn /
    * @return /
    */
    YcOrder findByOrderSn(String order_sn);
    /**
    * 根据 OrderSn 查询
    * @param order_sn /
    * @return /
    */
    List<YcOrder> findByOrderSnInAndOrderStatus(List<String> order_sn,Integer status);

    /**
     * 根据 父OrderSn 查询
     * @param order_sn /
     * @return /
     */
    List<YcOrder> findByFatherOrders(String order_sn);

    /**
     * 根据 配送人 查询
     * @param distributionid /
     * @return /
     */
    List<YcOrder> findByDistributionIdAndOrderStatus(Long distributionid,Integer status);


    /**
     * 根据 收货人
     * @param distributionid /
     * @return /
     */
    List<YcOrder> findByReceiverId(Long distributionid);



    @Query(nativeQuery=true,value="SELECT yo.* FROM yc_order as yo \n" +
            " LEFT JOIN yc_receiver_info as yri ON yo.receiver_id=yri.id \n" +
            " WHERE 1 = 1 \n" +
            " AND IF(ISNULL(:#{#yoc.orderSn}),1=1,:#{#yoc.orderSn} = yo.order_sn) \n" +
            " AND IF(ISNULL(:#{#yoc.userId}),1=1,:#{#yoc.userId} = yo.user_id) \n" +
            " AND IF(ISNULL(:#{#yoc.orderStatus}),1=1,:#{#yoc.orderStatus} = yo.order_status) \n" +
            " AND IF(ISNULL(:#{#yoc.supplierId}),1=1,:#{#yoc.supplierId} = yo.supplier_id) \n" +
            " AND IF(ISNULL(:#{#yoc.distributionId}),1=1,:#{#yoc.distributionId} = yo.distribution_id) \n" +
            " AND IF(ISNULL(:#{#yoc.isDistribution}),1=1,:#{#yoc.isDistribution} = yo.is_distribution) \n" +
            " AND IF(ISNULL(:#{#yoc.address}),1=1,yri.address LIKE CONCAT('%',:#{#yoc.address},'%')) \n" +
            " ORDER BY yo.create_time DESC \n" +
            " LIMIT :pageNumber,:pageSize\n")
    List<YcOrder> findAllSql(@Param("yoc") YcOrderQueryCriteria o,@Param("pageSize") int pageSize,@Param("pageNumber") int pageNumber);

    //List<YcOrder> findAllSql(YcOrderQueryCriteria criteria, int pageSize, int pageNumber);



    /**
     * 根据 订单号
     * @param orderSn /
     * @return /
     */
    List<YcOrder> findAllByOrderSnIsNotInAndUserId(Set<String> orderSn,Long userId);
}